In [20]:
import altair as alt
import pandas as pd
import numpy as np
alt.renderers.enable('notebook')

# matplotlib and seaborn for plotting
import matplotlib.pyplot as plt
import seaborn as sns

# for the notebook only (not for JupyterLab) run this command once per session
alt.renderers.enable('notebook')

# Suppress warnings from pandas
import warnings
warnings.filterwarnings('ignore')

plt.style.use('fivethirtyeight')
In [21]:
data = pd.read_csv('C:/Users/Naveen/Documents/globalterrorismdb_0718dist.csv',encoding ='ISO-8859-1')
data.head()
Out[21]:
year imonth iday country_txt region_txt provstate city latitude longitude success ... targtype1_txt targsubtype1_txt target1 natlty1_txt gname weaptype1_txt weapdetail nkill nwound property
0 1970 7 2 Dominican Republic Central America & Caribbean NaN Santo Domingo 18.456792 -69.951164 1 ... Property Named Civilian Julio Guzman Dominican Republic MANO-D Unknown NaN 1 0.0 0
1 1970 0 0 Mexico North America Federal Mexico city 19.371887 -99.086624 1 ... Government Diplomatic Personnel (outside of embassy, cons... Nadine Chaval, daughter Belgium 23rd of September Communist League Unknown NaN 0 0.0 0
2 1970 1 0 Philippines Southeast Asia Tarlac Unknown 15.478598 120.599741 1 ... Unknown Radio Journalist/Staff/Facility Employee United States Unknown Unknown NaN 1 0.0 0
3 1970 1 0 Greece Western Europe Attica Athens 37.997490 23.762728 1 ... Government Embassy/Consulate U.S. Embassy United States Unknown Explosives Explosive 0 0.0 1
4 1970 1 0 Japan East Asia Fukouka Fukouka 33.580412 130.396361 1 ... Government Embassy/Consulate U.S. Consulate United States Unknown Incendiary Incendiary 0 0.0 1

5 rows × 21 columns

In [22]:
year_success=data.groupby(['attacktype1_txt','year'])['success'].sum().unstack('attacktype1_txt')
year_success = year_success.reset_index().melt('year', var_name='AttackType', value_name='SuccessfulStrikes')

#Create a selection that chooses the nearest point & selects based on x-value
nearest = alt.selection(type='single', nearest=True, on='mouseover',
                        fields=['year'], empty='none')

# The basic line
line = alt.Chart().mark_line(interpolate='basis').encode(
    x='year:N',
    y='SuccessfulStrikes:Q',
    color='AttackType:N'
)

# Transparent selectors across the chart. This is what tells us
# the x-value of the cursor
selectors = alt.Chart().mark_point().encode(
    x='year:N',
    opacity=alt.value(0),
).add_selection(
    nearest
)

# Draw points on the line, and highlight based on selection
points = line.mark_point().encode(
    opacity=alt.condition(nearest, alt.value(1), alt.value(0))
)

# Draw text labels near the points, and highlight based on selection
text = line.mark_text(align='left', dx=5, dy=-5).encode(
    text=alt.condition(nearest, 'SuccessfulStrikes:Q', alt.value(' '))
)

# Draw a rule at the location of the selection
rules = alt.Chart().mark_rule(color='gray').encode(
    x='year:N',
).transform_filter(
    nearest
)

# Put the five layers into a chart and bind the data
alt.layer(line, selectors, points, rules, text,
          data=year_success, width=800, height=500)
Out[22]:

In the above graph we can see the number of terrorist attck which were success from 1972 to 2017. Here the colours represents the different attack types. As we can see the attacks had been gradually increase from the start of the year 2000 reaching peak around year 2014. The red line which represents the bombing/explosion is the most used type of attack by terrorists.

In [8]:
gr = data.groupby(['country_txt'])['success','nkill','nwound'].sum()
gr = gr.reset_index()
gr = gr.sort_values(by=['nkill','nwound'], ascending=False)
gr2 = gr[50:90]
In [9]:
pts = alt.selection(type="single", encodings=['x'])

rect = alt.Chart(gr2).mark_rect().encode(
    alt.X('nkill:Q', bin=True),
    alt.Y('nwound:Q', bin=True),
    alt.Color('count()',
        scale=alt.Scale(scheme='greenblue'),
        legend=alt.Legend(title='Total Records')
    )
)
circ = rect.mark_point().encode(
    alt.ColorValue('grey'),
    alt.Size('count()',
        legend=alt.Legend(title='Records in Selection')
    )
).transform_filter(
    pts
)

bar = alt.Chart(gr2).mark_bar().encode(
    x='country_txt:N',
    y='success:Q',
    color=alt.condition(pts, alt.ColorValue("steelblue"), alt.ColorValue("grey"))
).properties(
    width=800,
    height=200
).add_selection(pts)

alt.vconcat(
    rect + circ,
    bar
).resolve_legend(
    color="independent",
    size="independent"
)
Out[9]:

In [10]:
#################################### GRAPH 3 ##########################
tar_suc = data.groupby(['targtype1_txt','success', 'year'])['success'].agg(['count'])
tar_suc = tar_suc.reset_index()
In [11]:
base = alt.Chart(tar_suc).mark_bar().encode(
    x='success:N',
    y='count:Q',
    color='success:N',
    column='targtype1_txt:N'
).properties(
    width=80,
    height=400
)



year_slider = alt.binding_range(min=1970, max=2017, step=5)
slider_selection = alt.selection_single(bind=year_slider, fields=['year'], name="Year")


filter_year = base.add_selection(
    slider_selection
).transform_filter(
    slider_selection
)

filter_year
Out[11]:

In [12]:
#Plotly
import plotly
import plotly.plotly as py
import plotly.offline as offline
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
import cufflinks as cf
cf.set_config_file(offline=True)
map1 = data
#map1.head()
In [13]:
map1 = [go.Scattermapbox(
            lat= data['latitude'] ,
            lon= data['longitude'],
            #customdata = data['Event ID'],
            mode='markers',
            marker=dict(
                size= 3.5,
                color = 'red',
                opacity = .7,
            ),
          )]

layout = go.Layout(autosize=False,
                   mapbox= dict(accesstoken="pk.eyJ1Ijoic2hhejEzIiwiYSI6ImNqYXA3NjhmeDR4d3Iyd2w5M2phM3E2djQifQ.yyxsAzT94VGYYEEOhxy87w",
                                bearing=0,
                                pitch=50,
                                zoom=2,
                                center= dict(
                                         lat=12,
                                         lon=39),
                                style= "mapbox://styles/shaz13/cjk4wlc1s02bm2smsqd7qtjhs"),
                    width=900,
                    height=600, title = "Terrorist attack locations")

fig = dict(data=map1, layout=layout)
iplot(fig)
In [14]:
map2 = data

map2['nwound'] = map2['nwound'].fillna(0).astype(int)
map2['nkill'] = map2['nkill'].fillna(0).astype(int)
map2['fatlities'] = map2['nkill'] + map2['nwound']

map21 = map2.sort_values(by='fatlities',ascending=False)[:40]

heat=map21.pivot_table(index='country_txt',columns='year',values='fatlities')
heat.fillna(0,inplace=True)
In [15]:
import plotly.offline as py
py.init_notebook_mode(connected=True)
import plotly.graph_objs as go
colorscale = [[0, '#edf8fb'], [.3, '#00BFFF'],  [.6, '#8856a7'],  [1, '#810f7c']]
heatmap = go.Heatmap(z=heat.as_matrix(), x=heat.columns, y=heat.index, colorscale=colorscale)
data = [heatmap]
layout = go.Layout(
    title='Top 40 Worst Terror Attacks in History from 1982 to 2016',
    xaxis = dict(ticks='', nticks=20),
    yaxis = dict(ticks='')
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='heatmap',show_link=False)
In [16]:
map2_df = map2[(map2['targtype1_txt'] != 'Unknown') & (map2['fatlities'] > 50)]

map2_df = map2_df.sort_values(['region_txt', 'country_txt'])
map2_df = map2_df.drop(['latitude','longitude','target1'],axis=1)
map2_df = map2_df.dropna(subset=['city'])

hover_text = []
for index, row in map2_df.iterrows():
    hover_text.append(('City: {city}<br>'+
                      'targtype1_txt: {group}<br>'+
                      'fatlities: {fatlities}<br>'+
                      'year: {year}').format(city=row['city'],
                                            group=row['targtype1_txt'],
                                            fatlities=row['fatlities'],
                                            year=row['year']))
map2_df['text'] = hover_text
In [17]:
trace0 = go.Scatter(
    x=map2_df['year'][map2_df['country_txt'] == 'Iraq'],
    y=map2_df['fatlities'][map2_df['country_txt'] == 'Iraq'],
    mode='markers',
    name='Iraq',
    text=map2_df['text'][map2_df['country_txt'] == 'Iraq'],
    marker=dict(
        symbol='circle',
        sizemode='area',
        size=map2_df['fatlities'][map2_df['country_txt'] == 'Iraq'],
        line=dict(
            width=2
        ),
    )
)
trace1 = go.Scatter(
    x=map2_df['year'][map2_df['country_txt'] == 'Pakistan'],
    y=map2_df['fatlities'][map2_df['country_txt'] == 'Pakistan'],
    mode='markers',
    name='Pakistan',
    text=map2_df['text'][map2_df['country_txt'] == 'Pakistan'],
    marker=dict(
        symbol='circle',
        sizemode='area',
        size=map2_df['fatlities'][map2_df['country_txt'] == 'Pakistan'],
        line=dict(
            width=2
        ),
    )
)
trace2 = go.Scatter(
    x=map2_df['year'][map2_df['country_txt'] == 'Afghanistan'],
    y=map2_df['fatlities'][map2_df['country_txt'] == 'Afghanistan'],
    mode='markers',
    name='Afghanistan',
    text=map2_df['text'][map2_df['country_txt'] == 'Afghanistan'],
    marker=dict(
        symbol='circle',
        sizemode='area',
        size=map2_df['fatlities'][map2_df['country_txt'] == 'Afghanistan'],
        line=dict(
            width=2
        ),
    )
)
trace3 = go.Scatter(
    x=map2_df['year'][map2_df['country_txt'] == 'India'],
    y=map2_df['fatlities'][map2_df['country_txt'] == 'India'],
    mode='markers',
    name='India',
    text=map2_df['text'][map2_df['country_txt'] == 'India'],
    marker=dict(
        symbol='circle',
        sizemode='area',
        size=map2_df['fatlities'][map2_df['country_txt'] == 'India'],
        line=dict(
            width=2
        ),
    )
)
In [18]:
map2data = [trace0, trace1, trace2, trace3]
layout = go.Layout(
         title = 'The Big Four',
         xaxis = dict(
             title = 'Year',
             #type = 'log',
             range = [1976,2016],
             tickmode = 'auto',
             nticks = 30,
             showline = True,
             showgrid = False
             ),
         yaxis = dict(
             title = 'Fatlities',
             type = 'log',
             range = [1.8,3.6],
             tickmode = 'auto',
             nticks = 40,
             showline = True,
             showgrid = False),
         paper_bgcolor='rgb(243, 243, 243)',
         plot_bgcolor='rgb(243, 243, 243)',
         )

fig = go.Figure(data=map2data, layout=layout)
py.iplot(fig, filename='Terrorism Bubble')